package com.uqbar.game.random;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.uqbar.game.Bullet;
import com.uqbar.game.Spacecraft;
import com.uqbar.game.asteroid.tipe.Asteroid;
import com.uqbar.game.asteroid.tipe.GiantAsteroid;
import com.uqbar.game.asteroid.tipe.MediumAsteroid;
import com.uqbar.game.asteroid.tipe.SmallAsteroid;
import com.uqbar.game.scene.GameAsteroid;
import com.uqbar.game.scene.PlayScene;
import com.uqbar.vainilla.colissions.CollisionDetector;

public class RandomAsteroid {
	
	private PlayScene playScene;
	private Spacecraft spacecraft;
	private List<Asteroid> asteroids;
	private List<Asteroid> asteroidsAdd;
	
	public RandomAsteroid(PlayScene playScene){
		this.playScene = playScene;
		this.setAsteroids(new ArrayList<Asteroid>());
	}
	
	public void createLevel1(){
		createLevel(3);
	}
	
	private void createLevel(int cant){
		Random rand = new Random();
		
		
		for(int i= 0; i< cant; i++){
			
			int posX = rand.nextInt(600);
			int posY = rand.nextInt(600);
			double dirX = rand.nextDouble()-0.5;
			double dirY = rand.nextDouble()-0.5;
			int speed = rand.nextInt(50)+50;
			

			int asteroidType = rand.nextInt(3);
			Asteroid asteroid = null;
			
			if(asteroidType == 0){
				asteroid = new SmallAsteroid(posX, posY, speed, dirX, dirY);
			}
			if(asteroidType == 1){
				asteroid = new MediumAsteroid(posX, posY, speed, dirX, dirY, this);
			}
			if(asteroidType == 2){
				asteroid = new GiantAsteroid(posX, posY, speed, dirX, dirY, this);
			}
			
			asteroids.add(asteroid);
			
			playScene.getCurrentScene().addComponent(asteroid);
		}
	}
	

	public void add(List<Asteroid> as) {
		this.asteroidsAdd = as; 
	}

	public void collisionWith(Bullet bullet) {
		Asteroid astRemove = null;
		CollisionDetector collision = CollisionDetector.INSTANCE;
		
		for (Asteroid asteroid : asteroids) {
			if(collision.collidesCircleAgainstCircle(bullet.getX(), bullet.getY(), bullet.getDiameter(), 
					asteroid.getX(), asteroid.getY(), asteroid.getDiameter())){
				asteroid.exploit();
				bullet.getSoundExploitAsteroid().play();
				bullet.destroy();
				bullet.getSpacecraft().addScore(asteroid.getScore());
				astRemove = asteroid; 
			}
		}
		if(astRemove != null){
			this.asteroids.remove(astRemove);
		}
		if(this.asteroidsAdd != null){
			for (Asteroid asteroid : this.asteroidsAdd) {
				this.asteroids.add(asteroid);
			}
			this.asteroidsAdd = null;
		}
	}


//	public Asteroid getAstAdd() {
//		return astAdd;
//	}
//
//	public void addInList(Asteroid astAdd) {
//		this.astAdd = astAdd;
//	}
	
	public PlayScene getPlayScene() {
		return playScene;
	}

	public void PlayScene(PlayScene playScene) {
		this.playScene = playScene;
	}

	public Spacecraft getSpacecraft() {
		return spacecraft;
	}

	public void setAsteroids(List<Asteroid> asteroids) {
		this.asteroids = asteroids;
	}

	public List<Asteroid> getAsteroids() {
		return asteroids;
	}

	public void setSpacecraft(Spacecraft spacecraft) {
		this.spacecraft = spacecraft;
	}

	public void setAsteroidsAdd(List<Asteroid> asteroidsAdd) {
		this.asteroidsAdd = asteroidsAdd;
	}

	public List<Asteroid> getAsteroidsAdd() {
		return asteroidsAdd;
	}

	public boolean sizeEqualsZero() {
		return this.getAsteroids().size() == 0;
	}
}
